package com.pulaien.shop.controller;

import com.pulaien.common.annotation.Anonymous;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.domain.entity.SysUser;
import com.pulaien.common.utils.MessageUtils;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.system.domain.dto.customersetup.AdminModifyParam;
import com.pulaien.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author yuanlai
 * @create 2024-01-03 10:43
 **/
@RestController
@RequestMapping("/shop/h5/user")
@Api(tags = "H5商城端用户管理")
@Anonymous
public class ShopH5UserController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @ApiOperation("重置密码")
    @PostMapping(value = "/h5ResetPassword")
    public AjaxResult h5ResetPassword(@RequestBody AdminModifyParam modifyParam) {
        if (modifyParam.getEmail()==null){
            try{
                SysUser currentUser = SecurityUtils.getLoginUser().getUser();
                if (currentUser!=null){
                    modifyParam.setEmail(currentUser.getEmail());
                }else {
                    return AjaxResult.error(10048, MessageUtils.getMessage(10048));
                }
            }catch (Exception e){
                return AjaxResult.error(10048, MessageUtils.getMessage(10048));
            }
        }

        AjaxResult ajaxResult = userService.verifyCode(modifyParam, false);
        HashMap<String, String> data = (HashMap<String, String>) ajaxResult.get("data");

        String secretCode = data.get("secretCode");
        modifyParam.setSecretCode(secretCode);
        return userService.resetPassword(modifyParam, false);
    }
}
